<?php
namespace Admin\Controller;
use Think\Controller;
class LoginController extends Controller {
		//登陆
    public function login()
	{
	$t_agent=M('t_agent');

	if(IS_POST)
	{
			if($_POST['accounts']=="" || $_POST['password']=="")
			{
				echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
				echo "<script type='text/javascript'>alert('账号密码不能为空');location.href='".U('Login/login')."'</script>";die;
			}else
			{
				$verify= $_POST['verify'];
				$h=$this->check_verify($verify);
				if($h==0)
					{
					  	echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
						echo "<script type='text/javascript'>alert('请正确输入验证码');location.href='".U('Login/login')."'</script>";die;
					}
				$accounts=$_POST['accounts'];//接受POST传送过来的数据
				$password=md5($_POST['password']);//接受POST传送过来的密码
				$accounts_lenth=strlen($accounts);
				$isagree=$_POST['isagree'];
		
				if($accounts_lenth<11)
					{
						$row=$t_agent->where("accounts='$accounts' AND password='$password'")->find();
					if($_POST['remember']==1)
						{
							setcookie('accounts',trim($this->$accounts),3600*24*30);   // 指定密码保存一个月
							setcookie('password',trim($this->$password),3600*24*30);   // 指定密码保存一个月 
						}
					}
				elseif($accounts_lenth==11)
					{
						$row=$t_agent->where("phone='$accounts' AND password='$password'")->find();
					if($_POST['remember']==1)
						{
							setcookie('accounts',trim($this->$accounts),3600*24*30);   // 指定密码保存一个月
							setcookie('password',trim($this->$password),3600*24*30);   // 指定密码保存一个月 
						}
					}

				if($row)
				{
					session('accounts',$row['accounts']);
					session('password',$row['password']);
					session('username',$row['username']);
					session('nickname',$row['nickname']);
					session('userid',$row['userid']);
					session('phone',$row['phone']);
					session('agencylv',$row['agencylv']);
					session('login_time',$row['login_time']);
					session('gems',$row['gems']);
					$date['login_time']=time();
					$date['ip']=get_client_ip();
					$date['accounts']=$accounts;
					$t_agent->where('userid='.$row['userid'])->save();
					$this->redirect('Index/index'); 
				}else
				{
					echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
					echo "<script type='text/javascript'>alert('账号密码错误');location.href='".U('Login/login')."'</script>";die;
				}
			}
	}
		$this->display();	
    }
	
	public function Verify(){	
	$Verify =     new \Think\Verify();// 设置验证码字符为纯数字
	$Verify->codeSet = '0123456789';
	$Verify->length   = 4;
	$Verify->entry();
	}

	
	function check_verify($code, $id = ''){
		$verify = new \Think\Verify();
		return $verify->check($code, $id);
		}
	
	public function logint(){
		if(I('get.userid'))
		{
			session('accounts',null);
		}
		echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
		echo "<script type='text/javascript'>alert('退出成功');location.href='".U('Login/login')."'</script>";die;
	}
}

?>
